package club.ruanx.auth.filter;

import club.ruanx.auth.provider.AdminProvider;
import club.ruanx.auth.token.AdminToken;
import club.ruanx.security.provider.AbstractAuthenticationLoginFilterProvider;
import club.ruanx.security.provider.BaseDetailsAuthenticationProvider;
import club.ruanx.security.token.AuthenticationTokenGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

/**
 * @author 阮胜
 * @date 2018/9/14 22:11
 */
@Component
public class AdminAuthenticationLoginFilter extends AbstractAuthenticationLoginFilterProvider {

    private AdminProvider adminProvider;

    @Autowired
    @Qualifier("adminDetailService")
    public UserDetailsService userDetailsService;


    @Override
    public String obtainLoginUrl() {
        return "/admin/login";
    }

    @Override
    public UserDetailsService obtainUserDetailsService() {
        return userDetailsService;
    }

    @Override
    public BaseDetailsAuthenticationProvider obtainDetailsAuthenticationProvider() {
        if (adminProvider == null) {
            adminProvider = new AdminProvider();
        }
        return adminProvider;
    }

    @Override
    public AuthenticationTokenGenerator tokenGenerator() {
        return AdminToken::new;
    }

}
